<template>
	<view class="logins">
		<view class="container">
			<image src="https://ffa.firstui.cn/uploadfile/240801102825663.jpg" mode=""></image>
		</view>
		<button class="sss" @click="onButton">一键登录</button>
	</view>
	<view v-if="xianshi==='true'" class="loading">
		<fui-loading :isFixed="false" text="登录中"></fui-loading>
	</view>
	<fui-checkbox-group name="checkbox" v-model="vals" @change="change" class="bottoms">
		<view class="fui-list__item fui-list__item--align">
			<fui-label class="label-container">
				<fui-checkbox value="1" v-model="checked" color="#09BE4F" scaleRatio="0.7"></fui-checkbox>
			</fui-label>
			<p class="checkbox-label">
				已同意并阅读
				<text style="color: #09BE4F;" @click="onDetial">会员协议·隐私条款·会员条款</text>
			</p>
		</view>
	</fui-checkbox-group>

	<view class="bottom">
		<text>温馨提示：未注册至减的用户，初次登录时将完成注册</text>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue';
	import {
		url
	}
	from '@/utils/request'
	import {
		setMsgStorage
	} from '@/utils/setMsgStorage.js'
	import { onShow } from '@dcloudio/uni-app'
	const vals = ref([]); // 用来存储选中的值，这里初始化为空数组
	const checked = ref(true); // 默认设置为选中状态
	const getopneId = ref('')
	const xianshi = ref('false')
	const identify= ref('')
	const userPermissions = ref('')
	onShow(()=>{
		uni.clearStorageSync('identify')
	})
	
	const onButton = () => {

		if (vals.value == "1") {
			xianshi.value = 'true';
			wx.getUserProfile({
				desc: '用于完善会员资料',
				success: (Res) => {
					console.log("我是" + Res.userInfo.nickName)
					setMsgStorage("avatarUrl", Res.userInfo.avatarUrl);
					setMsgStorage("nickName", Res.userInfo.nickName);
					wx.login({
						success(res) {
							if (res.code) {

								wx.request({
									url: url + '/wx/login',
									method: 'post',
									data: {
										code: res.code,
										userImg: Res.userInfo.avatarUrl,
										userName: Res.userInfo.nickName,
										userNumber: "123456+"
									},
									success: (loginRes) => {
										//todo 登陆成功
										//存入用户信息缓存
										getopneId.value = loginRes.data.data.openId
										
										console.log("woshj"+loginRes.data.data.userPermissions)
										uni.setStorage({
											key: 'openId',
											data: getopneId.value,
											success: function() {
												console.log('success');
											}
										});

									
										if (loginRes.data.data.userPermissions === 0 ) {
											uni.switchTab({
												url: '/pages/index/index'
											});
											wx.showToast({
												title: '成功',
												icon: 'success',
												duration: 2000
											})
										} else if (loginRes.data.data.userPermissions === 1) {
											uni.redirectTo ({
												url: '/pages/qishoudingdan/qishoudingdan'
											});
											wx.showToast({
												title: '成功',
												icon: 'success',
												duration: 2000
											})
											
										} 

						
									

								
									},
									fail: () => {
										xianshi.value = 'false'
										'false'; // Hide loading spinner on failure
									}
								})
							} else {
								console.log('登录失败！' + res.errMsg)
							}
						}
					})
				}
			})

		} else {
			uni.showToast({
				title: '请点击同意',
				//将值设置为 success 或者直接不用写icon这个参数
				icon: 'error',
				//显示持续时间为 2秒
				duration: 2000
			})
		}
	}
	const change = (event) => {
		console.log('选中的值：', vals.value);
		console.log('事件对象：', event);
	}
	const onDetial = () => {
		uni.navigateTo({
			url: '../detail/detail'
		});
	}
</script>

<style scoped>
	.loading {
		position: fixed;
		left: 50%;
		top: 285px;
		transform: translate(-50%, -50%);
		z-index: 111111;
	}

	.logins {
		height: 53vh;
		background: #FFFFFF;
		border-radius: 14px;
		margin: 0px 12px;
	}

	.container {
		display: flex;
		width: 35vh;
		justify-content: center;
		align-items: center;
		height: 42vh;
		margin: 0 auto;
	}

	.sss {
		width: 80%;
		height: 49px;
		line-height: 49px;
		text-align: center;
		font-size: 16px;
		color: #FFFFFF;
		letter-spacing: 1px;
		background-color: #09BE4F;
		border: none;
		border-radius: 24px;
	}

	.bottoms {
		left: 72px;
		position: fixed;
		bottom: 36px;
		width: 100%;
		font-size: 12px;
		color: #91908e;
		text-align: center;
		padding: 10px 0;
	}

	.bottom {
		position: fixed;
		bottom: 10px;
		width: 100%;
		font-size: 12px;
		color: #91908e;
		text-align: center;
		padding: 10px 0;
	}

	.fui-list__item--align {
		display: flex;
		align-items: center;
	}

	.label-container {
		display: flex;
		align-items: center;
	}

	.checkbox-container {
		display: flex;
		align-items: center;
		/* Align items vertically */
	}

	.checkbox-label {
		margin-left: 8px;
		/* Adjust the spacing as needed */
	}
</style>